import RTLog from '../../modules/rt_log.js';
import Count from '../../modules/count';

Page({
    mpid: '',
    data: {
        userInfo: {},
        wxworkQrcode: '',
        countObj: {
            count_ad: 0,
            count_followed: 0,
            count_uv: 0,
            customErrorPopup: false,
            errContext: ''
        }
    },
    onLoad: function (options) {
        this.mpid = options.mpid || '';
        if (options.wxworkqrcode && options.wxworkqrcode != '') {
            this.setData({
                wxworkQrcode: options.wxworkqrcode
            })
        }
        let userInfo = wx.getStorageSync('__session__');
        let parseUserInfo = userInfo ? JSON.parse(userInfo) : {};
        this.setData({
            userInfo: parseUserInfo
        })
    },
    onReady() {
        this.getCount();
    },
    previewImg() {
        wx.previewImage({
            urls: [this.data.wxworkQrcode]
        });
    },
    getCount() {
        Promise.all([
            Count.request('GET', 'count_ad'),
            Count.request('GET', 'count_followed'),
            Count.request('GET', 'count_uv')
        ])
            .then((res) => {
                if (res.length > 1) {
                    this.setData({
                        countObj: {
                            count_ad: res[0].count,
                            count_followed: res[1].count,
                            count_uv: res[2].count
                        }
                    });
                }
            })
            .catch((err) => {
                // getApp().mox.stat(`get_all_count fail: ${err.msg || err.errMsg || '未知错误'}`);
                getApp().mox.stat(`get_all_count fail`);
                RTLog.error('get_all_count fail:', err.errMsg || err.msg || '未知错误');
            });
    },
    deleteCount() {
        wx.showLoading({
            title: '删除中...',
            mask: true
        });
        Promise.all([
            Count.request('DELETE', 'count_ad'),
            Count.request('DELETE', 'count_followed'),
            Count.request('DELETE', 'count_uv')
        ])
            .then(() => {
                wx.hideLoading();
                wx.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 1500,
                    mask: false
                });
                this.returnHome();
            })
            .catch((err) => {
                // getApp().mox.stat(`delete_all_count fail: ${err.msg || err.errMsg || '未知错误'}`);
                getApp().mox.stat(`delete_all_count fail`);
                RTLog.error('delete_all_count fail:', err.errMsg || err.msg || '未知错误');
                this.setData({
                    errContext: err.errMsg || err.msg || '未知错误',
                    customErrorPopup: true
                });
            });
    },
    deleteUserInfo() {
        wx.removeStorageSync('__session__');
        this.returnHome();
    },
    deleteAllCache() {
        wx.clearStorage();
        this.returnHome();
    },
    returnHome() {
        setTimeout(() => {
            wx.reLaunch({
                url: '/pages/index/index'
            });
        }, 1200);
    }
});
